import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:my_jingdongshop/controller/checkOutController.dart';
import '../../controller/cartController.dart';
import '../../services/CartServices.dart';
import '../../services/ScreenAdapter.dart';
import '../../services/UserServices.dart';
import '../../widget/MySnackbar.dart';
import '../Cart/CartItem.dart';

class CartPage extends StatefulWidget {
  const CartPage({super.key});

  @override
  State<CartPage> createState() => _CartPageState();
}

class _CartPageState extends State<CartPage> {
  // 是否编辑
  bool _isEdit = true;

  CartController cartController = Get.find<CartController>();
  CheckoutController checkoutController = Get.find<CheckoutController>();

  //去结算
  doCheckOut() async {
    //1、获取购物车选中的数据
    List checkOutData = await CartServices.getCheckOutData();
    //2、保存购物车选中的数据
    checkoutController.changeCheckOutListData(checkOutData);
    //3、购物车有没有选中的数据
    if (checkOutData.isNotEmpty) {
      //4、判断用户有没有登录
      var loginState = await UserServices.getUserLoginState();
      if (loginState) {
        Get.toNamed("/checkOut");
      } else {
        showSnackbar(message: "您还没有登录，请登录以后再去结算");
        Get.toNamed("/login");
      }
    } else {
      showSnackbar(message: "购物车没有选中的数据");
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);

    return Scaffold(
      appBar: AppBar(
        title: const Text("购物车"),
        actions: [
          InkWell(
            onTap: () {
              setState(() {
                _isEdit = !_isEdit;
              });
            },
            child: const IconButton(
              icon: Icon(
                Icons.edit,
                color: Colors.black,
              ),
              onPressed: null,
            ),
          )
        ],
      ),
      body: Obx(() {
        return cartController.cartList.isNotEmpty
            ? SafeArea(
                child: Stack(
                  children: [
                    Padding(
                      padding: const EdgeInsets.only(bottom: 50),
                      child: ListView(
                        children: cartController.cartList.map((value) {
                          return CartItem(value);
                        }).toList(),
                      ),
                    ),
                    Positioned(
                      width: ScreenAdapter.width(750),
                      height: ScreenAdapter.height(100),
                      bottom: 0,
                      child: Container(
                        decoration: const BoxDecoration(
                          border: Border(
                              top: BorderSide(width: 1, color: Colors.black12)),
                          color: Colors.white,
                        ),
                        width: ScreenAdapter.width(750),
                        height: ScreenAdapter.height(100),
                        padding: const EdgeInsets.all(5),
                        child: Stack(
                          children: [
                            Align(
                              alignment: Alignment.centerLeft,
                              child: Row(
                                children: [
                                  SizedBox(
                                    width: ScreenAdapter.width(60),
                                    child: Checkbox(
                                      value: cartController.isCheckedAll.value,
                                      activeColor: Colors.pink,
                                      onChanged: (val) {
                                        cartController.changeCheckAll(val!);
                                      },
                                    ),
                                  ),
                                  const Text(
                                    "全选",
                                  ),
                                  const SizedBox(width: 20),
                                  const Text(
                                    "合计：",
                                  ),
                                  Text(
                                    "￥${cartController.allPrice.value}",
                                    style: TextStyle(
                                        fontSize: ScreenAdapter.size(36),
                                        fontWeight: FontWeight.bold,
                                        color: Colors.red),
                                  )
                                ],
                              ),
                            ),
                            Align(
                              alignment: Alignment.centerRight,
                              child: _isEdit
                                  ? ElevatedButton(
                                      onPressed: doCheckOut,
                                      style: ElevatedButton.styleFrom(
                                        backgroundColor: Colors.red,
                                        fixedSize: const Size(100, 40),
                                      ),
                                      child: const Text(
                                        "结算",
                                        style: TextStyle(
                                            // fontSize: ScreenAdapter.size(20),
                                            color: Colors.white),
                                      ),
                                    )
                                  : ElevatedButton(
                                      style: ElevatedButton.styleFrom(
                                        backgroundColor: Colors.blue,
                                        fixedSize: const Size(100, 40),
                                      ),
                                      child: const Text(
                                        "删除",
                                        style: TextStyle(
                                          color: Colors.white,
                                          // fontSize: ScreenAdapter.size(20),
                                        ),
                                      ),
                                      onPressed: () {
                                        cartController.removeItem();
                                      },
                                    ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              )
            : const Center(
                child: Text("购物车为空"),
              );
      }),
    );
  }
}
